/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Presentation;

import com.toedter.calendar.JDateChooser;
import BussinesLogic.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;


public class GUIdirDeportivo extends javax.swing.JFrame {
   
    private JPanel panelAux;
    private String logoReturn;
    private String idEvento;
    
    public GUIdirDeportivo(String idEvento) {
        logoReturn = "";
        this.idEvento = idEvento;
        panelAux = new JPanel();
        initComponents();
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        barraMenu = new javax.swing.JMenuBar();
        menuEventos = new javax.swing.JMenu();
        itemCrearEvento = new javax.swing.JMenuItem();
        itemRegDisciplina = new javax.swing.JMenuItem();
        menuDeportistas = new javax.swing.JMenu();
        itemRegDeportista = new javax.swing.JMenuItem();
        menuArbitros = new javax.swing.JMenu();
        itemRegArbitro = new javax.swing.JMenuItem();
        itemAsignarComp = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JOGOS2016 - Director Deportivo");
        setResizable(false);

        menuEventos.setText("Evento Deportivo");

        itemCrearEvento.setText("Crear Evento");
        itemCrearEvento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemCrearEventoActionPerformed(evt);
            }
        });
        menuEventos.add(itemCrearEvento);

        itemRegDisciplina.setText("Registrar Disciplina");
        itemRegDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemRegDisciplinaActionPerformed(evt);
            }
        });
        menuEventos.add(itemRegDisciplina);

        barraMenu.add(menuEventos);

        menuDeportistas.setText("Deportistas");

        itemRegDeportista.setText("Registrar Deportista");
        itemRegDeportista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemRegDeportistaActionPerformed(evt);
            }
        });
        menuDeportistas.add(itemRegDeportista);

        barraMenu.add(menuDeportistas);

        menuArbitros.setText("Arbitros");

        itemRegArbitro.setText("Registrar Arbitro");
        itemRegArbitro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemRegArbitroActionPerformed(evt);
            }
        });
        menuArbitros.add(itemRegArbitro);

        itemAsignarComp.setText("Asignar Competencia");
        itemAsignarComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemAsignarCompActionPerformed(evt);
            }
        });
        menuArbitros.add(itemAsignarComp);

        barraMenu.add(menuArbitros);

        setJMenuBar(barraMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemCrearEventoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemCrearEventoActionPerformed
        // TODO add your handling code here:  
        this.remove(panelAux);
        logoReturn = "";

        JPanel panel = new JPanel();
        panel.setName("panelAux");
        panel.setLayout(new java.awt.GridLayout(9,2));
        
        JLabel lTitulo = new JLabel("CREAR EVENTO ");
        lTitulo.setHorizontalAlignment(SwingConstants.RIGHT);
        JLabel lNombre = new JLabel("NOMBRE");
        JLabel lDescripcion = new JLabel("DESCRIPCION");
        JLabel lfechaInicio = new JLabel("FECHA DE INICIO");
        JLabel lfechaFin = new JLabel("FECHA DE FINALIZACION");
        JLabel lPais = new JLabel("PAIS");
        JLabel lCiudad = new JLabel("CIUDAD");
        
        JTextField campoNombre = new JTextField(40);
        JTextField campoDescripcion = new JTextField(250);
        JDateChooser calFechaInicio = new JDateChooser();
        JDateChooser calFechaFin = new JDateChooser();
        JTextField campoPais = new JTextField(20);
        JTextField campoCiudad = new JTextField(20);
        JButton botonLogo = new JButton("Escoger Logo");
        botonLogo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JFileChooser chooserLogo = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "JPG & GIF Images", "jpg", "gif");
                chooserLogo.setFileFilter(filter);
                int returnFile = chooserLogo.showOpenDialog(panel);
                if(returnFile==JFileChooser.APPROVE_OPTION){
                    logoReturn = chooserLogo.getSelectedFile().getAbsolutePath();
                }
            }
        });
        
        JButton botonReg = new JButton("CREAR EVENTO");
        botonReg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                crearEventoActionPerformed(campoNombre.getText(), campoDescripcion.getText(), calFechaInicio.getDate(),
                        calFechaFin.getDate(), campoPais.getText(), campoCiudad.getText(), logoReturn);
            }
        });
        
        panel.add(lTitulo);
        panel.add(new JLabel("DEPORTIVO"));
        panel.add(lNombre);
        panel.add(campoNombre);
        panel.add(lDescripcion);
        panel.add(campoDescripcion);
        panel.add(lfechaInicio);
        panel.add(calFechaInicio);
        panel.add(lfechaFin);
        panel.add(calFechaFin);
        panel.add(lPais);
        panel.add(campoPais);
        panel.add(lCiudad);
        panel.add(campoCiudad);
        panel.add(botonLogo);
        panel.add(botonReg);
        
        panelAux = panel;
        this.setLayout(new java.awt.GridLayout(1,1));
        add(panelAux);
        
        this.revalidate();
        this.repaint();
    }//GEN-LAST:event_itemCrearEventoActionPerformed

    private void itemRegDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemRegDisciplinaActionPerformed
        // TODO add your handling code here:
        this.remove(panelAux);
        logoReturn = "";
        JPanel panel = new JPanel();
        panel.setName("panelAux");
        panel.setLayout(new java.awt.GridLayout(4,2));
        
        JLabel lTitulo = new JLabel("REGISTRAR DISCI");
        lTitulo.setHorizontalAlignment(SwingConstants.RIGHT);
        JLabel lNombre = new JLabel("NOMBRE");
        JLabel lDescripcion = new JLabel("DESCRIPCION");
        
        JTextField campoNombre = new JTextField(40);
        JTextField campoDescripcion = new JTextField(250);
        JButton botonLogo = new JButton("Escoger Logo");
        botonLogo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JFileChooser chooserLogo = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "JPG & GIF Images", "jpg", "gif");
                chooserLogo.setFileFilter(filter);
                int returnFile = chooserLogo.showOpenDialog(panel);
                if(returnFile==JFileChooser.APPROVE_OPTION){
                    logoReturn = chooserLogo.getSelectedFile().getAbsolutePath();
                }
            }
        });
        
        JButton botonReg = new JButton("REGISTRAR DISCIPLINA");
        botonReg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                registrarDisciplinaActionPerformed(campoNombre.getText(), campoDescripcion.getText(), logoReturn);
            }
        });
        
        panel.add(lTitulo);
        panel.add(new JLabel("PLINA DEPORTIVA"));
        panel.add(lNombre);
        panel.add(campoNombre);
        panel.add(lDescripcion);
        panel.add(campoDescripcion);
        panel.add(botonLogo);
        panel.add(botonReg);
        
        JPanel panelprinc = new JPanel();
        JPanel p = new JPanel();
        
        panelprinc.setLayout(new java.awt.GridLayout(2,1));
        panelprinc.add(panel);
        panelprinc.add(p);
        
        panelAux = panelprinc;       
        this.setLayout(new java.awt.GridLayout(1,1));
        add(panelAux);
                
        this.revalidate();
        this.repaint();
    }//GEN-LAST:event_itemRegDisciplinaActionPerformed

    private void itemRegDeportistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemRegDeportistaActionPerformed
        // TODO add your handling code here:
        this.remove(panelAux);
        logoReturn = "";
        
        BL_Disciplina bl_disciplina = new BL_Disciplina();
        Vector v = (Vector) bl_disciplina.consultarDisciplinaPorEvento(idEvento).getObject();
        
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridLayout(11,2));
        
        JTextField campoId = new JTextField(20);
        JTextField campoNombre = new JTextField(25);
        JTextField campoApellido = new JTextField(25);
        JTextField campoPaisNac = new JTextField(25);
        JDateChooser calFechaNac = new JDateChooser();
        JTextField campoEstatura = new JTextField(3);
        JTextField campoPeso = new JTextField(3);
        JTextField campoPaisRep = new JTextField(25);
        JComboBox campoDisciplina = new JComboBox(v);
        JButton botonFoto = new JButton("Escoger Foto");
        botonFoto.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JFileChooser chooserLogo = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "JPG & GIF Images", "jpg", "gif");
                chooserLogo.setFileFilter(filter);
                int returnFile = chooserLogo.showOpenDialog(panel);
                if(returnFile==JFileChooser.APPROVE_OPTION){
                    logoReturn = chooserLogo.getSelectedFile().getAbsolutePath();
                }
            }
        });
        
        JButton botonReg = new JButton("REGISTRAR  DEPORTISTA");
        botonReg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                registrarDeportistaActionPerformed(campoId.getText(), campoNombre.getText(), campoApellido.getText(), 
                        campoPaisNac.getText(), calFechaNac.getDate(), campoEstatura.getText(), campoPeso.getText(),
                        campoPaisRep.getText(), campoDisciplina.getSelectedItem().toString(), logoReturn);
            }
        });
        
        JLabel lTitulo = new JLabel("REGISTRAR ");
        lTitulo.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(lTitulo);
        panel.add(new JLabel("DEPORTISTA"));
        panel.add(new JLabel("IDENTIFICACION"));
        panel.add(campoId);
        panel.add(new JLabel("NOMBRE"));
        panel.add(campoNombre);
        panel.add(new JLabel("APELLIDO"));
        panel.add(campoApellido);
        panel.add(new JLabel("PAIS DE NACIMIENTO"));
        panel.add(campoPaisNac);
        panel.add(new JLabel("FECHA DE NACIMIENTO"));
        panel.add(calFechaNac);
        panel.add(new JLabel("ESTATURA(mtrs)"));
        panel.add(campoEstatura);
        panel.add(new JLabel("PESO(kg)"));
        panel.add(campoPeso);
        panel.add(new JLabel("PAIS QUE REPRESENTA"));
        panel.add(campoPaisRep);
        panel.add(new JLabel("DISCIPLINA"));
        panel.add(campoDisciplina);
        panel.add(botonFoto);
        panel.add(botonReg);
        
        panelAux = panel;
        this.setLayout(new java.awt.GridLayout(1,1));
        add(panelAux);
        
        this.revalidate();
        this.repaint();
        
    }//GEN-LAST:event_itemRegDeportistaActionPerformed

    private void itemRegArbitroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemRegArbitroActionPerformed
        // TODO add your handling code here:
        this.remove(panelAux);
        
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridLayout(11,2));
        
        JTextField campoId = new JTextField(20);
        JTextField campoNombre = new JTextField(25);
        JTextField campoApellido = new JTextField(25);
        JTextField campoPaisNac = new JTextField(25);
        JDateChooser calFechaNac = new JDateChooser();
        JTextField campoUsername = new JTextField();
        JPasswordField campoPassword = new JPasswordField();
                
        JButton botonReg = new JButton("REGISTRAR ARBITRO");
        botonReg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                registrarArbitroActionPerformed(campoId.getText(), campoNombre.getText(), campoApellido.getText(), 
                        campoPaisNac.getText(), calFechaNac.getDate(), campoUsername.getText(), campoPassword.getText());
            }
        });
        
        JLabel lTitulo = new JLabel("REGISTRAR ");
        lTitulo.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(lTitulo);
        panel.add(new JLabel("ARBITRO"));
        panel.add(new JLabel("IDENTIFICACION"));
        panel.add(campoId);
        panel.add(new JLabel("NOMBRE"));
        panel.add(campoNombre);
        panel.add(new JLabel("APELLIDO"));
        panel.add(campoApellido);
        panel.add(new JLabel("PAIS DE NACIMIENTO"));
        panel.add(campoPaisNac);
        panel.add(new JLabel("FECHA DE NACIMIENTO"));
        panel.add(calFechaNac);
        panel.add(new JLabel("USUARIO"));
        panel.add(campoUsername);
        panel.add(new JLabel("CONTRASEÑA"));
        panel.add(campoPassword);
        panel.add(new JLabel(""));
        panel.add(botonReg);        
        
        panelAux = panel;
        this.setLayout(new java.awt.GridLayout(1,1));
        add(panelAux);
        
        this.revalidate();
        this.repaint();
        
    }//GEN-LAST:event_itemRegArbitroActionPerformed

    private void itemAsignarCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemAsignarCompActionPerformed
        // TODO add your handling code here:
        this.remove(panelAux);
        
        Vector vArbitros = (Vector) new BL_Arbitro().consultarArbitros().getObject();
        Vector vCompetencias = (Vector) new BL_Competencia().consultarCompetencias().getObject();
        
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridLayout(4,2));
        
        JComboBox campoArbitro = new JComboBox(vArbitros);
        JComboBox campoCompetencia = new JComboBox(vCompetencias);
        JButton botonAsignar = new JButton("ASIGNAR");
        botonAsignar.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                asignarArbitroCompetencia(campoArbitro.getSelectedItem().toString(),
                        campoCompetencia.getSelectedItem().toString());
            }
        });
        
        JLabel lTitulo = new JLabel("ASIGNAR ARBITRO A ");
        lTitulo.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(lTitulo);
        panel.add(new JLabel("COMPETENCIA"));
        panel.add(new JLabel("COMPETENCIA"));
        panel.add(campoCompetencia);
        panel.add(new JLabel("ARBITRO"));
        panel.add(campoArbitro);
        panel.add(new JLabel(""));
        panel.add(botonAsignar);
        
        JPanel panelprinc = new JPanel();
        JPanel p = new JPanel();
        
        panelprinc.setLayout(new java.awt.GridLayout(2,1));
        panelprinc.add(panel);
        panelprinc.add(p);
        panelAux = panelprinc;
        this.setLayout(new java.awt.GridLayout(1,1));
        add(panelAux);
        
        this.revalidate();
        this.repaint();
    }//GEN-LAST:event_itemAsignarCompActionPerformed

    private void crearEventoActionPerformed(String campoNombre, String campoDescripcion, Date calFechaInicio,
            Date calFechaFin, String campoPais, String campoCiudad, String logo){
        
        if(campoNombre.isEmpty() || campoDescripcion.isEmpty() || calFechaInicio==null || calFechaFin==null
                || campoPais.isEmpty() || campoCiudad.isEmpty() || logo.isEmpty()){
            JOptionPane.showMessageDialog(this,"Por favor diligenciar todos los campos del formulario"
                    + " y elegir un logo para el evento","JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            BL_EventoDeportivo bl_evento = new BL_EventoDeportivo();
            
            Request rqst = bl_evento.crearEvento(campoNombre, campoDescripcion, calFechaInicio, calFechaFin, campoPais, 
                    campoCiudad, logo);
            
            JOptionPane.showMessageDialog(this,rqst.getMessage(),
                    "JOGOS2016 - Mensaje",JOptionPane.INFORMATION_MESSAGE);

            if (rqst.getTransac()) {
                logoReturn="";
                this.remove(panelAux);
                this.revalidate();
                this.repaint();
            }
        }
    }
    
    private void registrarDisciplinaActionPerformed(String campoNombre, String campoDescripcion,String logo){
        
        if(campoNombre.isEmpty() || campoDescripcion.isEmpty() ||logo.isEmpty()){
            JOptionPane.showMessageDialog(this,"Por favor diligenciar todos los campos del formulario"
                    + " y elegir un logo para la disciplina","JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            BL_Disciplina bl_disciplina = new BL_Disciplina();
            
            Request rqst = bl_disciplina.registrarDisciplina(campoNombre, campoDescripcion, logo, idEvento);
            
            JOptionPane.showMessageDialog(this,rqst.getMessage(),
                    "JOGOS2016 - Mensaje",JOptionPane.INFORMATION_MESSAGE);

            if (rqst.getTransac()) {
                logoReturn="";
                this.remove(panelAux);
                this.revalidate();
                this.repaint();
            }
        }
    }
    
    private void registrarDeportistaActionPerformed(String id, String nombre, String apellido, String paisNac,
            Date fechaNac, String estatura, String peso, String paisRep, String disciplina, String foto){
        
        if(id.isEmpty() || nombre.isEmpty() || apellido.isEmpty() || paisNac.isEmpty() || fechaNac==null ||
                estatura.isEmpty() || peso.isEmpty() || paisRep.isEmpty() || foto.isEmpty()){
            JOptionPane.showMessageDialog(this,"Por favor diligenciar todos los campos del formulario"
                    + " y elegir una foto para el deportista","JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            
            BL_Deportista bl_deportista = new BL_Deportista();
            
            Request rqst = bl_deportista.registrarDeportista(id, nombre, apellido, paisNac, fechaNac, estatura, peso,
                    disciplina, foto, paisRep);
            
            JOptionPane.showMessageDialog(this,rqst.getMessage(),
                    "JOGOS2016 - Mensaje",JOptionPane.INFORMATION_MESSAGE);

            if (rqst.getTransac()) {
                logoReturn="";
                this.remove(panelAux);
                this.revalidate();
                this.repaint();
            }
        }
    }
    
    private void registrarArbitroActionPerformed(String id, String nombre, String apellido, String paisNac,
            Date fechaNac, String username, String password){
        
        if(id.isEmpty() || nombre.isEmpty() || apellido.isEmpty() || paisNac.isEmpty() || fechaNac==null ||
                username.isEmpty() || password.isEmpty()){
            JOptionPane.showMessageDialog(this,"Por favor diligenciar todos los campos del formulario"
                    + " y elegir una foto para el deportista","JOGOS2016 - Error", JOptionPane.ERROR_MESSAGE);
        }
        else{
            
            BL_Arbitro bl_arbitro = new BL_Arbitro();
            
            Request rqst = bl_arbitro.registrarArbitro(id, nombre, apellido, paisNac, fechaNac, username, password);
            
            JOptionPane.showMessageDialog(this,rqst.getMessage(),
                    "JOGOS2016 - Mensaje",JOptionPane.INFORMATION_MESSAGE);

            if (rqst.getTransac()) {
                logoReturn="";
                this.remove(panelAux);
                this.revalidate();
                this.repaint();
            }
        }
    }
    
    private void asignarArbitroCompetencia(String arbitro, String competencia){
        
        Request rqst = new Request();
        BL_Arbitro bl_arbitro = new BL_Arbitro();
        bl_arbitro.asignarArbitroCompetencia(arbitro, competencia);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIdirDeportivo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIdirDeportivo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIdirDeportivo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIdirDeportivo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIdirDeportivo("1-8-2014-2-9-2014").setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar barraMenu;
    private javax.swing.JMenuItem itemAsignarComp;
    private javax.swing.JMenuItem itemCrearEvento;
    private javax.swing.JMenuItem itemRegArbitro;
    private javax.swing.JMenuItem itemRegDeportista;
    private javax.swing.JMenuItem itemRegDisciplina;
    private javax.swing.JMenu menuArbitros;
    private javax.swing.JMenu menuDeportistas;
    private javax.swing.JMenu menuEventos;
    // End of variables declaration//GEN-END:variables
}
